<?php

/**
 * infosUser actions.
 *
 * @package    appcemhti
 * @subpackage infosUser
 * @author     Steve Cancès
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class infosUserActions extends sfActions
{
    /**
    * Executes index action
    *
    * @param sfRequest $request A request object
    */
    public function executeIndex(sfWebRequest $request)
    {
        $this->sf_guard_user = $this->getUser()->getGuardUser();
        $this->user = Doctrine_Core::getTable('user')->find($this->getUser()->getGuardUser()->getId());
        $this->sf_user = $this->getUser();
    }
    
    public function executeEdit(sfWebRequest $request)
    {
        $this->sf_guard_user = $this->getUser()->getGuardUser();
        $this->form = new sfGuardUserForm($this->sf_guard_user);
    }

    public function executeUpdate(sfWebRequest $request)
    {
        $this->sf_guard_user = $this->getUser()->getGuardUser();
        $this->form = new sfGuardUserForm($this->sf_guard_user);
        $groups= array();
        $iGroups = 0;
        foreach($this->sf_guard_user->getGroups() as $group)
        {
            $groups[$iGroups] = $group->getId();
            $iGroups = $iGroups+1;
        }
        
        $params = $request->getParameter($this->form->getName());
        $params['updated_at'] = date('Y-m-d H:i:s', time());
        $params['groups_list'] = $groups;
        $request->setParameter($this->form->getName(), $params);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        //On récupère les paramètres du form
        $params = $request->getParameter($form->getName());
        //On récupère le debut de l'adresse mail
        $username = explode("@", $params['email_address']);
        $params['username'] = $username[0];
        $request->setParameter($form->getName(), $params);

        $form->bind($params, $request->getFiles($form->getName()));
        if ($form->isValid())
        {
            $sf_guard_user = $form->save();
            $user = $sf_guard_user->getProfile();
            $user->setLogin($params['username']);
            $user->save();
            
            //$user = Doctrine_Core::getTable('user')->find($this->getUser()->getGuardUser()->getId());
            //$user->setIdUser($sf_guard_user->getId());
            //$user->setLogin($params['username']);
            //$user->save();
            $this->redirect('infosUser/index');
        }
    }
    
    public function executeListMesLots(sfWebRequest $request)
    {
        if ($request->isXmlHttpRequest())
        {
            $sSearch = $request->getParameter("sSearch");
            $iSortCol_0 = $request->getParameter("iSortCol_0");
            $sSortDir_0 = $request->getParameter("sSortDir_0");
            $iDisplayStart = $request->getParameter("iDisplayStart");
            $iDisplayLength = $request->getParameter("iDisplayLength");
            $page = number_format($iDisplayStart / $iDisplayLength) + 1;
            
            
            $q = Doctrine_Core::getTable('lot')->getLotParUtilisateurParDroitDatatable($this->getUser()->getGuardUser()->getId(), "ecriture", $sSearch, $iSortCol_0, $sSortDir_0);
            
            $this->pager = new sfDoctrinePager(
                'lot',
                sfConfig::get(2)
            );
            $this->pager->setQuery($q);
            $this->pager->setMaxPerPage($iDisplayLength);
            $this->pager->setMaxRecordLimit($iDisplayLength);
            $this->pager->setPage($page);
            $this->pager->init();
            
            $pager = $this->pager;

            $aaData = array();
            $list = $pager->getResults();
            
            
            foreach ($list as $v)
            {                
                $consulter = '<a class="submit" href="lot/show/id_lot/' . $v->getIdLot() . '">Consulter</a>';
                if($v->getConfidentiel() == 1) $conf = "Oui"; else $conf = "Non";
                $fournisseur = "";
                if($v->getIdFournisseur()) $fournisseur = $v->getFournisseur()->getNom();
                $user = $v->getUser()->getSfGuardUser();
                $date = "";
                if($v->getDateRecep()) $date = $v->getDateTimeObject('date_recep')->format('d/m/Y');
                $aaData[] = array(
                    "0" => $v->getIdLot(),
                    "1" => $v->getNom(),
                    "2" => $v->getMatiere()->getLibelle(),
                    "3" => $v->getPolytype(),
                    "4" => $v->getEchantillon()->count(),
                    "5" => $fournisseur,
                    "6" => $date,
                    "7" => $user->getFirstName() . " " . $user->getLastName(),
                    "8" => $conf,
                    "9" => $v->getCommentaire()->count(),
                    "10" => $consulter,
                );
            }

            $output = array(
            "iTotalRecords" => count($pager),
            "iTotalDisplayRecords" => count($pager),
            "aaData" => $aaData,
            );

            return $this->renderText(json_encode($output));
        }
    }
    
    public function executeListMesEchantillons(sfWebRequest $request)
    {
        if ($request->isXmlHttpRequest())
        {
            $sSearch = $request->getParameter("sSearch");
            $iSortCol_0 = $request->getParameter("iSortCol_0");
            $sSortDir_0 = $request->getParameter("sSortDir_0");
            $iDisplayStart = $request->getParameter("iDisplayStart");
            $iDisplayLength = $request->getParameter("iDisplayLength");
            $page = number_format($iDisplayStart / $iDisplayLength) + 1;
            
            $q = Doctrine_Core::getTable('Echantillon')->getEchantillonParUtilisateurParDroitDatatable(
                    $this->getUser()->getGuardUser()->getId(),
                    "ecriture",
                    $sSearch,
                    $iSortCol_0,
                    $sSortDir_0
            );
            $this->pager = new sfDoctrinePager(
                'echantillon',
                sfConfig::get(2)
            );
            $this->pager->setQuery($q);
            $this->pager->setMaxPerPage($iDisplayLength);
            $this->pager->setMaxRecordLimit($iDisplayLength);
            $this->pager->setPage($page);
            $this->pager->init();
            
            $pager = $this->pager;

            $aaData = array();
            $list = $pager->getResults();
            foreach ($list as $v)
            {
                $forme = "";
                if($v->getTypeForme())
                {
                    $forme = $v->getTypeForme()->getLibelle();
                }
                $lieu = "";
                if($v->getLieu()) $lieu = $v->getLieu()->getLibelle();
                $date = "";
                if($v->getDateCreation()) $date = $v->getDateTimeObject('date_creation')->format('d/m/Y');
                $consulter = '<a class="submit" href="echantillon/show/id_echantillon/' . $v->getIdEchantillon() . '">Consulter</a>';
                $aaData[] = array(
                    "0" => $v->getIdEchantillon(),
                    "1" => $v->getNom(),
                    "2" => $v->getLot()->getNom(),
                    "3" => $date,
                    "4" => $v->getEtatEchantillon()->getLibelle(),
                    "5" => $lieu,
                    "6" => $forme,
                    "7" => $v->getNbTraitement(),
                    "8" => $v->getMesure()->count(),
                    "9" => $v->getCommentaire()->count(),
                    "10" => $consulter
                );
            }

            $output = array(
            "iTotalRecords" => count($pager),
            "iTotalDisplayRecords" => count($pager),
            "aaData" => $aaData,
            );

            return $this->renderText(json_encode($output));
       }
    }
    
    protected function responseJSON($status, $help, $msg)
    {
        $response = array(
            "status" => $status,
            "help" => $help,
            "msg" => $msg
        );
        return $this->renderText(json_encode($response));
    }
    
    public function executeHelp(sfWebRequest $request)
    {
        if ($request->isXmlHttpRequest())
        {
            $help = $request->getParameter("help");
            if($help == 1)
            {
                $this->getUser()->setAttribute("help", "on");
                return $this->responseJSON("success", $help, "Les info-bulles sont maintenant activées");
            }
            elseif($help == 0)
            {
                $this->getUser()->setAttribute("help", "off");
                return $this->responseJSON("success", $help, "Les info-bulles sont maintenant désactivées");
            }
            else
            {
                return false;
            }
            return true;
        }
        else return false;
    }
}
